﻿#include <fstream>
#include "Reader.h"
#include "json.hpp"
#include "Prompt.h"
#include "Menu.h"
#include "io.h"

using namespace nlohmann;

list<Reader> Reader::ReaderList = list<Reader>();

/**
 * @brief 创建读者对象
 */
Reader::Reader(int id, string name, string password)
{
	this->id = id;
	this->name = name;
	this->password = password;
};
/**
 * @brief 判断两个读者对象是否为同一人
 */
bool operator==(const Reader& a, const Reader& b)
{
	return a.id == b.id;
};
/**
 * @brief 重载输出
 *
 * @param output
 * @param b
 * @return ostream&
 */
ostream& operator<<(ostream& output, const Reader& r)
{
	output << "读者证id：\t" << r.id << "读者姓名：\t" << r.name;
	return output;
}
/**
 * @brief 获取下一个图书的id号
 *
 * @return int
 */
int Reader::getNextReaderId()
{
	int max_id = 0;
	for (auto iter = ReaderList.begin(); iter != ReaderList.end(); iter++)
	{
		if (iter->id > max_id)
			max_id = iter->id;
	}
	return max_id + 1;
}
/**
 * @brief 转化为json字符的表示
 *
 * @return string
 */
string Reader::toJson()
{
	json json_array = json::array();
	for (auto iter = ReaderList.begin(); iter != ReaderList.end(); iter++)
	{
		json json_item = { {"id", iter->id}, {"name", iter->name}, {"password", iter->password} };
		json_array.push_back(json_item);
	}
	return json_array.dump();
}
/**
 * @brief json 字符串转数组
 *
 * @return string
 */
list<Reader> Reader::fromJson(string json_str)
{
	json json_array = json::parse(json_str);
	list<Reader> reader_list;
	for (auto iter = json_array.begin(); iter != json_array.end(); iter++)
	{
		Reader r = Reader((*iter)["id"], (*iter)["name"], (*iter)["password"]);
		reader_list.push_back(r);
	}
	return reader_list;
}

/**
 * @brief 根据读者证id号查读者
 *
 * @return Reader*
 */
Reader* Reader::queryById(int reader_id)
{
	for (auto r = ReaderList.begin(); r != ReaderList.end(); r++)
	{
		if (r->id == reader_id)
			return &*r;
	}
	return nullptr;
}
/**
 * @brief 根据读者证姓名查读者
 *
 * @return Reader*
 */
Reader* Reader::queryByName(string reader_name)
{
	for (auto r = ReaderList.begin(); r != ReaderList.end(); r++)
	{
		if (r->name == reader_name)
			return &*r;
	}
	return nullptr;
}

/**
 * @brief 根据id记录删除读者
 *
 * @param reader_id
 * @return int 1 表示不存在删除失败，0表示删除成功
 */
int Reader::delById(int reader_id)
{
	Reader* r = queryById(reader_id);
	if (r != nullptr)
	{
		ReaderList.remove(*r);
		return 0;
	}
	else
	{
		return 1;
	}
}

/**
 * @brief 添加读者
 *
 */
void Reader::addReader()
{
	string id_string;
	int id = -1;
	string name, password;
	for (; id == -1 || Reader::queryById(id) != nullptr;)
	{
		id_string = prompt("请输入读者证号：");
		id = atoi(id_string.c_str());
	}
	name = prompt("请输入读者姓名：");
	password = prompt_passoword("请输入读者卡密码：");
	Reader r(id, name, password);
	ReaderList.push_back(r);
	cout << "读者添加成功" << endl;
	saveFile();
}

/**
 * @brief 输出读者列表
 *
 */
void Reader::listReader()
{
	cout << toJson() << endl;
}

void Reader::delReader()
{
	Menu menu("请选择删除依据", { "id", "name" });
	Menu::MenuSelection del_option = menu.show();
	Reader* reader_to_del = nullptr;
	if (del_option == 1)
	{
		// 按照id删除
		int id;
		string id_string = prompt("请输入要删除的读者id：");
		id = atoi(id_string.c_str());
		reader_to_del = queryById(id);
	}
	else if (del_option == 2)
	{
		// 按照姓名删除第一个匹配
		string name_string = prompt("请输入要删除的读者姓名：");
		reader_to_del = queryByName(name_string);
	}
	// 判断是否查找到读者
	if (reader_to_del != nullptr)
	{
		cout << "成功删除读者：id=" << reader_to_del->id << " name=" << reader_to_del->name << endl;
		delById(reader_to_del->id);
	}
	else
	{
		cout << "读者不存在！";
	}
	saveFile();
}

const char* Reader::file_name = "data/readers.txt";

/**
 * @brief 将读者列表写入文件
 *
 */
void Reader::saveFile()
{
	ofstream outfile;
	outfile.open(file_name, ios::out);
	outfile << toJson() << endl;
	outfile.close();
}
/**
 * @brief 从文件加载数据
 *
 */
void Reader::loadFromFile()
{
	cout << "加载读者记录" << endl;
	if (_access(file_name, 0) == 0)
	{
		ifstream infile;
		string json_str;
		infile.open(file_name, ios::out);
		infile >> json_str;
		infile.close();
		ReaderList = fromJson(json_str);
		cout << "加载了" << ReaderList.size() << "条读者记录";
	}
	else
	{
		cout << "没有读者列表文件" << endl;
	}
}
